package com.baseproject.widget;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.baseproject.BaseApplication;
import com.baseproject.R;
import com.baseproject.util.DeviceUtils;

/**
 * 自定义加载对话框
 *
 * @author Administrator
 */
public class FrameAnimatorProgressDialog extends Dialog {

    public FrameAnimatorProgressDialog(Context context, int theme) {
        super(context, theme);
    }

    static FrameAnimatorProgressDialog mFrameAnimatorProgressDialog;
    static ImageView imageView;

    /**
     * 创建加载对话框
     *
     * @param context 上下文对象
     * @return 对话框实例
     */
    private static FrameAnimatorProgressDialog getLoadingDialog(Context context) {
        mFrameAnimatorProgressDialog = new FrameAnimatorProgressDialog(context, R.style.customProgressDialog);
        mFrameAnimatorProgressDialog.setContentView(R.layout.progress_dialog_view);
        if (mFrameAnimatorProgressDialog == null) {
            return null;
        }
        Window window = mFrameAnimatorProgressDialog.getWindow();
        // 设置加载框的属性，使它弹出的时候屏幕不会变暗
        WindowManager.LayoutParams lp = window.getAttributes();
        window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        lp.dimAmount = 0f;
        lp.gravity = Gravity.CENTER;
        window.setAttributes(lp);
        lp.width = DeviceUtils.getScreenWidth(context);
        lp.height = DeviceUtils.getScreenHeight(context);
        mFrameAnimatorProgressDialog.setCancelable(true);
        imageView = (ImageView) mFrameAnimatorProgressDialog.findViewById(R.id.progress_loading_anim);
        return mFrameAnimatorProgressDialog;
    }

    /**
     * 设置对话框的加载信息
     *
     * @param strMessage 信息内容
     */
    public static void setMessage(String strMessage) {
        ((TextView) mFrameAnimatorProgressDialog.findViewById(R.id.message)).setText(strMessage);
    }

    public static void showInCurrentActivity() {
        BaseApplication baseApplication = BaseApplication.getApplication();
        if (null != baseApplication) {
            show(baseApplication.getCurrentShowingActivity());
        }

    }

    public static void show(Context context) {
        show(context, true);
    }

    public static void show(Context context, boolean cancelable) {
        if (null == context) {
            return;
        }
        _dismiss();
        //拿到dialog
        mFrameAnimatorProgressDialog = getLoadingDialog(context);
        if (mFrameAnimatorProgressDialog == null) {
            return;
        }
        //设置点击外围是否消失
        mFrameAnimatorProgressDialog.setCancelable(cancelable);

        AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground();
        animationDrawable.start();
        mFrameAnimatorProgressDialog.show();
    }

    public static void _dismiss() {
        if (null != imageView) {
            AnimationDrawable anim = (AnimationDrawable) (imageView.getBackground());
            anim.stop();
            imageView = null;
        }
        if (mFrameAnimatorProgressDialog != null && mFrameAnimatorProgressDialog.isShowing()) {
            mFrameAnimatorProgressDialog.dismiss();
        }
        mFrameAnimatorProgressDialog = null;
    }
}
